/**
 * 
 */
package net.microlinktech.testing.ui.model;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import javax.swing.table.DefaultTableModel;

import net.microlinktech.testing.bean.Meter;

/**
 * @author zhangqing
 *
 */
public class MeterTableModel extends DefaultTableModel {
	
	private static final long serialVersionUID = 1L;

	private static final String[] columnIds = { "中心节点编号","中心节点地址", "版本号","子节点编号","子节点地址", "设置地址状态","事件上报状态","网络节点档案数目","在网子节点数目", "温度传感器示值","湿度传感器示值","PH传感器示值", "CO2浓度",
			"土壤浓度传感器示值"};


	/*public MeterTableModel(INetworkService networkService) {
		this.networkService = networkService;
	}*/
	
	public MeterTableModel(){
		
	}

	private List<Meter> meters = null;

	public void init() {
		meters = new ArrayList<>();
	}

	@Override
	public Object getValueAt(int row, int column) {
		if (row >= getRowCount()) {
			return new Object();
		}
		Meter lamp = getMeter(row);
		switch (column) {
		case 0:
			return meters.indexOf(lamp) + 1;
		case 1:
			return lamp.getNodeAddress();
		case 2:
			return lamp.getVersionNo();
		case 3:
			return lamp.getSubnodeNo();
		case 4:
			return lamp.getSubnodeAddress();
		case 5:
			return lamp.getAddressState();
		case 6:
			return lamp.getReportState();
		case 7:
			return lamp.getRecordNum();
		case 8:
			return lamp.getOnlineNum();
		case 9:
			return lamp.getTemperatureVal();
		case 10:
			return lamp.getHumidityVal();
		case 11:
			return lamp.getPhVal();
		case 12:
			return lamp.getCarbonVal();
		case 13:
			return lamp.getSoilDensity();
		/*case 14:
			return lamp.getUc();
		case 15:
			return lamp.getIc();
		case 16:
			return lamp.getLc();
		case 17:
			return lamp.getPc();
		case 18:
			return lamp.getPsum();*/
		}
		return null;
	}

	public void add(Meter lamp) {
		int index = meters.size();
		meters.add(lamp);
		fireTableRowsInserted(index, index);
	}

	public List<Meter> getMeters() {
		return meters;
	}

	public Meter getMeter(int row) {
		return meters.get(row);
	}

	public void setMeters(List<Meter> lamps) {
		this.meters.clear();
		this.meters.addAll(lamps);
		fireTableDataChanged();
	}

	@Override
	public int getRowCount() {
		return meters != null ? meters.size() : 0;
	}

	@Override
	public String getColumnName(int column) {
		return columnIds[column];
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return columnIndex > 0;
	}

	@Override
	public int getColumnCount() {
		return columnIds.length;
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		Meter meter = meters.get(rowIndex);
		String atten = (String) value;
		if (meter != null) {
			switch (columnIndex) {
			case 1:
				meter.setNodeAddress(atten);
				break;
			case 2:
				meter.setVersionNo(atten);
				break;
			case 3:
				meter.setSubnodeNo(Integer.parseInt(atten));
				break;
			case 4:
				meter.setSubnodeAddress(atten);
				break;
			case 5:
				meter.setAddressState(atten);
				break;
			case 6:
				meter.setReportState(atten);
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void removeRow(int arg0) {
		this.meters.remove(arg0);
		this.fireTableRowsDeleted(arg0, arg0);
	}
	

	 
}
